using System;
using HCore.Core;
using HCore.Unity.Core;

namespace HCore.Tests.Runtime.Tilemap
{
    public enum TilemapSprite
    {
        None,
        Ground,
        Path,
        Dirt
    }

    public class TilemapGridObject
    {
        private readonly Grid2D<TilemapGridObject> mGrid2D;

        private readonly int mX;
        private readonly int mY;
        private TilemapSprite mTilemapSprite;

        public TilemapGridObject(Grid2D<TilemapGridObject> grid2D, int x, int y)
        {
            mGrid2D = grid2D;
            mX = x;
            mY = y;
        }

        public void SetTilemapSprite(TilemapSprite tilemapSprite, bool triggerGridObjectChange = true)
        {
            if (Objects.EqualsForNullSafe(tilemapSprite, mTilemapSprite)) return;
            mTilemapSprite = tilemapSprite;
            if (triggerGridObjectChange) mGrid2D.TriggerCellChanged(mX, mY);
        }

        public TilemapSprite GetTilemapSprite()
        {
            return mTilemapSprite;
        }

        public override string ToString()
        {
            return $"{mTilemapSprite}";
        }

        public SaveObject Save()
        {
            return new SaveObject
            {
                sprite = mTilemapSprite,
                x = mX,
                y = mY
            };
        }

        public void Load(SaveObject saveObject)
        {
            mTilemapSprite = saveObject.sprite;
        }

        [Serializable]
        public class SaveObject
        {
            public TilemapSprite sprite;
            public int x;
            public int y;
        }
    }
}